package com.hg.oa.module.calendar.layer;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hg.oa.R;
import com.hg.oa.common.BaseFragment;
import com.hg.oa.module.calendar.model.BaseDayItemInfo;
import com.hg.oa.module.calendar.model.DayReportInfo;
import com.hg.oa.module.calendar.presenter.ApplyPagePressenter;
import com.hg.oa.util.DateUtil;
import com.hg.oa.widget.CalendarViewDialog;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 填写日报
 * Created by fengxiang on 2016/9/12.
 */
public class ApplyDayReportFragment extends BaseFragment implements CalendarViewDialog.OnDateSelectedListener {

    String tag = "DayReportFragment";
    Unbinder unbinder;

    @BindView(R.id.select_type)
    MaterialBetterSpinner spinner_type;

    @BindView(R.id.edit_worktime)
    MaterialEditText edit_worktime;

    @BindView(R.id.edit_date)
    MaterialEditText edit_date;

    @BindView(R.id.edit_work_content)
    MaterialEditText edit_work_content;

    ApplyPagePressenter pressenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_apply_day_report, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        pressenter = new ApplyPagePressenter(getActivity(), this);
        pressenter.setSelectedDate(DateUtil.DateToString(new Date(), DateUtil.DateStyle.YYYY_MM_DD), edit_date);
        pressenter.loadSelectTypeData(spinner_type);

        edit_date.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    pressenter.showCalendarViewDialog();
                }
            }
        });

        edit_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pressenter.showCalendarViewDialog();
            }
        });

        edit_worktime.addTextChangedListener(
                new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        pressenter.validateWorkTime(s, edit_worktime);
                    }
                }
        );
        init(getArguments());
        return rootView;
    }


    private void init(Bundle bundle) {
        if (bundle != null) {
            BaseDayItemInfo info = (BaseDayItemInfo) bundle.get("info");
            initData(info);
        }

    }

    private void initData(BaseDayItemInfo info) {
        if (info != null && info instanceof DayReportInfo) {
            DayReportInfo dayReportInfo = (DayReportInfo) info;
            spinner_type.setText(dayReportInfo.workItem);
            edit_date.setText(dayReportInfo.workDate);
            edit_worktime.setText(dayReportInfo.workTime);
            edit_work_content.setText(dayReportInfo.workContent);
        }
    }

    @Override
    public void onSelected(String date) {
        Log.i(tag, date);
        pressenter.setSelectedDate(date, edit_date);
    }
}
